package gob.inei.enaho.controller.activity;

import util.exception.ServiceException;
import util.form.respuesta.RegistroImpl;
import util.patron.command.Command2;
import android.os.AsyncTask;

public class CargarMarcoTask 
extends AsyncTask<RegistroImpl,Integer,Exception>
{
	private CargarMarcoActivity activity;
	
	public CargarMarcoTask(CargarMarcoActivity activity) {
		this.activity = activity;
	}

	
	
	@Override
	protected Exception doInBackground(RegistroImpl... params) {
		try {
			activity.getDataXmlMarcoService().cargarMarco(params[0],
					new Command2<Integer, Integer>() {			
						@Override
						public void execute(Integer tipoOperacion, Integer i) throws Exception {
							publishProgress(1,tipoOperacion,i);
						}
					},
					new Command2<Integer, Integer>() {			
						@Override
						public void execute(Integer tipoOperacion, Integer i) throws Exception {
							publishProgress(2,tipoOperacion,i);
						}
					});
		} catch (Exception e) {
			return e;
		}
		
		return null;
	}
	

	@Override
	protected void onProgressUpdate(Integer... values) {
		int t = values[0];
		int op = values[1];
		int i = values[2];
		
		if(t==1) {
			if(op==1)
				activity.getTvConglomerado1().setText("Leyendo "+i+" conglomerados");
			else
				activity.getTvConglomerado2().setText("Grabando "+i+" conglomerados");
		}
		else if(t==2) {
			if(op==1)
				activity.getTvViviendaMarco1().setText("Leyendo "+i+" viviendas");
			else
				activity.getTvViviendaMarco2().setText("Grabando "+i+" viviendas");
		}
	}

	@Override
	protected void onPostExecute(Exception result) {
		if(result!=null) {			
			if(result instanceof ServiceException) {
				activity.getTvConglomerado1().setText(result.getMessage());
			}
			else {
				activity.manejarExcepcion(result);
			}
			
		}
	}
	
	

}
